package com.haixiaoke.saas.lock.service.impl;

import java.time.*;
import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.constant.DeviceLockConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.DefaultEnum;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.lock.domain.DeviceLock;
import com.haixiaoke.saas.lock.domain.Group;
import com.haixiaoke.saas.lock.domain.LockFace;
import com.haixiaoke.saas.lock.mapper.LockFaceMapper;
import com.haixiaoke.saas.lock.service.*;
import com.haixiaoke.saas.user.domain.User;
import com.haixiaoke.saas.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 锁人脸授权Service业务层处理
 *
 * @author panda
 * @date 2025-04-02
 */
@Service
public class LockFaceServiceImpl implements ILockFaceService {
    @Autowired
    private LockFaceMapper lockFaceMapper;
    @Autowired
    private IDeviceLockService deviceLockService;
    @Autowired
    private IUserService userService;
    @Autowired
    private LockDevicesService lockDevicesService;
    @Autowired
    private ILockOperatingRecordService lockOperatingRecordService;
    @Autowired
    private ILockPwdService lockPwdService;
    @Autowired
    private IGroupService groupService;

    /**
     * 查询锁人脸授权
     *
     * @param faceId 锁人脸授权主键
     * @return 锁人脸授权
     */
    @Override
    public LockFace selectLockFaceByFaceId(Long faceId) {
        return lockFaceMapper.selectLockFaceByFaceId(faceId);
    }

    /**
     * 查询锁人脸授权列表
     *
     * @param lockFace 锁人脸授权
     * @return 锁人脸授权
     */
    @Override
    public List<LockFace> selectLockFaceList(LockFace lockFace) {
        return lockFaceMapper.selectLockFaceList(lockFace);
    }

    /**
     * 新增锁人脸授权
     *
     * @param lockFace 锁人脸授权
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertLockFace(LockFace lockFace, Long loginUserId) {
        AjaxResult ajaxResult = this.checkParam(lockFace);
        if (ajaxResult != null) {
            return ajaxResult;
        }
        LocalDate nowLocalDate = LocalDate.now();
        ZonedDateTime zonedDateTime = lockFace.getAuthStartTime().toInstant().atZone(ZoneId.systemDefault());
        if (nowLocalDate.isAfter(zonedDateTime.toLocalDate())) {
            return AjaxResult.error("授权开始时间不可小于当前时间");
        }
        Date nowDate = DateUtils.getNowDate();
        DeviceLock lock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(lock)) {
            return AjaxResult.error("锁不存在，请联系管理员");
        }
        LockFace face = lockFaceMapper.selectLockFaceByLockIdAndUserId(lockFace.getLockId(), loginUserId);
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, face.getAuthRoleType()) &&
                StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("主管理员不能添加其他主管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, face.getAuthRoleType()) &&
                !StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MEMBER, lockFace.getAuthRoleType())) {
            return AjaxResult.error("管理员只能添加成员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_TIME_TYPE_LONG, lockFace.getAuthTimeType())) {
            LocalDateTime startDateTime = zonedDateTime.toLocalDateTime();
            LocalDateTime endDateTime = startDateTime.plusYears(20)
                    .with(LocalTime.MAX); // 直接设置为当天的最后一刻
            lockFace.setAuthEndTime(Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (StrUtil.isNotBlank(lockFace.getUserPhone())) {
            User user = userService.selectUserByPhone(lockFace.getUserPhone());
            Group group;
            if (ObjectUtil.isEmpty(user)) {
                user = new User();
                user.setPhone(lockFace.getUserPhone());
                user.setNickName("未命名");
                user.setUserName(lockFace.getUserName());
                user.setCreateTime(nowDate);
                userService.insertUser(user);

                group = new Group();
                group.setGroupName("默认");
                group.setUserId(user.getUserId());
                group.setGroupType(DeviceLockConstants.GROUP_TYPE_NON_DEFAULT);
                group.setCreateBy(face.getUserName());
                groupService.insertGroup(group);
            } else {
                group = groupService.selectDefaultGroupByUserId(user.getUserId());
            }
            lockFace.setUserId(user.getUserId());
            lockFace.setGroupId(group.getGroupId());
        }
        lockFace.setCreateTime(nowDate);
        String identification = lockDevicesService.getIdentification();
        lockFace.setIdentification(identification);
        if (StrUtil.isNotBlank(lockFace.getFaceImage())) {
            lockDevicesService.createLockFace(lockFace.getFaceImage(), lock.getLockMac(), identification, lockFace.getAuthStartTime(), lockFace.getAuthEndTime());
        }
        int result = lockFaceMapper.insertLockFace(lockFace);

        if (result > 0) {
            lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), face.getUserName(),
                    DeviceLockConstants.OPERATING_TYPE_ADD, "新增授权人脸：" + lockFace.getUserName());
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 新增锁人脸授权
     *
     * @param lockFace 锁人脸授权
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult manageInsertLockFace(LockFace lockFace) {
        AjaxResult ajaxResult = this.checkParam(lockFace);
        if (ajaxResult != null) {
            return ajaxResult;
        }
        LocalDate nowLocalDate = LocalDate.now();
        ZonedDateTime zonedDateTime = lockFace.getAuthStartTime().toInstant().atZone(ZoneId.systemDefault());
        if (nowLocalDate.isAfter(zonedDateTime.toLocalDate())) {
            return AjaxResult.error("授权开始时间不可小于当前时间");
        }
        Date nowDate = DateUtils.getNowDate();
        DeviceLock lock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(lock)) {
            return AjaxResult.error("锁不存在，请联系管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_TIME_TYPE_LONG, lockFace.getAuthTimeType())) {
            LocalDateTime startTime = lockFace.getAuthStartTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            LocalDateTime endDateTime = startTime.plusYears(20)
                    .with(LocalTime.MAX); // 直接设置为当天的最后一刻
            lockFace.setAuthEndTime(Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (StrUtil.isNotBlank(lockFace.getUserPhone())) {
            User user = userService.selectUserByPhone(lockFace.getUserPhone());
            Group group;
            if (ObjectUtil.isEmpty(user)) {
                user = new User();
                user.setPhone(lockFace.getUserPhone());
                user.setNickName("未命名");
                user.setUserName(lockFace.getUserName());
                user.setCreateTime(nowDate);
                userService.insertUser(user);

                group = new Group();
                group.setGroupName("默认");
                group.setUserId(user.getUserId());
                group.setGroupType(DeviceLockConstants.GROUP_TYPE_NON_DEFAULT);
                group.setCreateBy(lockFace.getUserName());
                groupService.insertGroup(group);
            } else {
                group = groupService.selectDefaultGroupByUserId(user.getUserId());
            }
            lockFace.setUserId(user.getUserId());
            lockFace.setGroupId(group.getGroupId());
        }
        lockFace.setCreateTime(nowDate);
        String identification = lockDevicesService.getIdentification();
        lockFace.setIdentification(identification);
        if (StrUtil.isNotBlank(lockFace.getFaceImage())) {
            lockDevicesService.createLockFace(lockFace.getFaceImage(), lock.getLockMac(), identification, lockFace.getAuthStartTime(), lockFace.getAuthEndTime());
        }
        int result = lockFaceMapper.insertLockFace(lockFace);

        if (result > 0) {
            lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), lockFace.getUserName(),
                    DeviceLockConstants.OPERATING_TYPE_ADD, "新增授权人脸：" + lockFace.getUserName());
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public int insertLockFace(LockFace lockFace) {
        return lockFaceMapper.insertLockFace(lockFace);
    }

    /**
     * 修改锁人脸授权
     *
     * @param lockFace 锁人脸授权
     * @return 结果
     */
    @Override
    public AjaxResult updateLockFace(LockFace lockFace, Long loginUserId) {
        AjaxResult ajaxResult = this.checkParam(lockFace);
        if (ajaxResult != null) {
            return ajaxResult;
        }
        lockFace.setIdentification(null);
        if (StrUtil.equals(DeviceLockConstants.AUTH_TIME_TYPE_LONG, lockFace.getAuthTimeType())) {
            LocalDateTime startTime = lockFace.getAuthStartTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime();
            LocalDateTime endDateTime = startTime.plusYears(20)
                    .with(LocalTime.MAX); // 直接设置为当天的最后一刻
            lockFace.setAuthEndTime(Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (lockFace.getFaceId() == null) {
            return AjaxResult.error("授权用户数据异常，请联系管理员");
        }
        DeviceLock lock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(lock)) {
            return AjaxResult.error("锁不存在，请联系管理员");
        }
        LockFace faceByFaceId = lockFaceMapper.selectLockFaceByFaceId(lockFace.getFaceId());
        if (ObjectUtil.isEmpty(faceByFaceId)) {
            return AjaxResult.error("锁人脸授权不存在，请联系管理员");
        }
        Date nowDate = DateUtils.getNowDate();
        boolean flag = StrUtil.isNotBlank(faceByFaceId.getFaceImage());
        LockFace face = lockFaceMapper.selectLockFaceByLockIdAndUserId(lockFace.getLockId(), loginUserId);
        if (!Objects.equals(lockFace.getFaceId(), face.getFaceId()) &&
                !StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, face.getAuthRoleType()) &&
                StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("管理员不能修改主管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, face.getAuthRoleType()) &&
                StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("管理员不能修改其他用户为主管理员");
        }
        if (StrUtil.isNotBlank(lockFace.getUserPhone())) {
            User user = userService.selectUserByPhone(lockFace.getUserPhone());
            Group group;
            if (ObjectUtil.isEmpty(user)) {
                user = new User();
                user.setPhone(lockFace.getUserPhone());
                user.setNickName("未命名");
                user.setUserName(lockFace.getUserName());
                user.setCreateTime(nowDate);
                userService.insertUser(user);

                group = new Group();
                group.setGroupName("默认");
                group.setUserId(user.getUserId());
                group.setGroupType(DeviceLockConstants.GROUP_TYPE_NON_DEFAULT);
                group.setCreateBy(face.getUserName());
                groupService.insertGroup(group);
            } else {
                group = groupService.selectDefaultGroupByUserId(user.getUserId());
            }
            lockFace.setUserId(user.getUserId());
            lockFace.setGroupId(group.getGroupId());
        }
        if (StrUtil.isBlank(lockFace.getFaceImage()) && StrUtil.isNotBlank(faceByFaceId.getFaceImage())) {
            lockDevicesService.removeFace(lock.getLockMac(), faceByFaceId.getIdentification());
        }
        if (StrUtil.isNotBlank(lockFace.getFaceImage())) {
            if (!StrUtil.equals(lockFace.getFaceImage(), faceByFaceId.getFaceImage())) {
                if (StrUtil.equals(DeviceLockConstants.LOCK_STATE_ENABLE, faceByFaceId.getFaceState())) {
                    if (flag) {
                        lockDevicesService.updateLockFace(lockFace.getFaceImage(), lock.getLockMac(), faceByFaceId.getIdentification(), lockFace.getAuthStartTime(), lockFace.getAuthEndTime());
                    } else {
                        lockDevicesService.createLockFace(lockFace.getFaceImage(), lock.getLockMac(), faceByFaceId.getIdentification(), lockFace.getAuthStartTime(), lockFace.getAuthEndTime());
                    }
                }
            }
        }
        lockFace.setUpdateTime(DateUtils.getNowDate());
        int result = lockFaceMapper.updateLockFaceByImage(lockFace);

        // 添加操作记录
        if (result > 0) {
            lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), face.getUserName(),
                    DeviceLockConstants.OPERATING_TYPE_EDIT, "编辑授权：" + lockFace.getUserName());
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 批量删除锁人脸授权
     *
     * @param faceIds 需要删除的锁人脸授权主键
     * @return 结果
     */
    @Override
    public int deleteLockFaceByFaceIds(Long[] faceIds) {
        return lockFaceMapper.deleteLockFaceByFaceIds(faceIds);
    }

    /**
     * 删除锁人脸授权信息
     *
     * @param faceId 锁人脸授权主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteLockFaceByFaceId(Long faceId, Long loginUserId, String userName) {
        LockFace lockFace = lockFaceMapper.selectLockFaceByFaceId(faceId);
        if (ObjectUtil.isEmpty(lockFace)) {
            return AjaxResult.error("锁人脸授权不存在，请联系管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("不能删除主管理员");
        }
        DeviceLock deviceLock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(deviceLock)) {
            return AjaxResult.error("锁不存在，请联系管理员");
        }
        if (loginUserId != null) {
            LockFace face = this.selectLockFaceByLockIdAndUserId(deviceLock.getLockId(), loginUserId);
            if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, face.getAuthRoleType()) &&
                    StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, lockFace.getAuthRoleType())) {
                return AjaxResult.error("管理员不能删除管理员");
            }
            userName = face.getUserName();
        }
        if (StrUtil.isNotBlank(lockFace.getFaceImage()) &&
                StrUtil.equals(DeviceLockConstants.LOCK_STATE_ENABLE, lockFace.getFaceState())) {
            lockDevicesService.removeFace(deviceLock.getLockMac(), lockFace.getIdentification());
        }
        int result = lockFaceMapper.softDeleteLockFaceByFaceId(faceId);

        // 添加操作记录
        if (result > 0) {
            lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), userName,
                    DeviceLockConstants.OPERATING_TYPE_DELETE, "删除授权：" + lockFace.getUserName());
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public LockFace selectLockFaceByLockIdAndUserName(Long lockId, String userName) {
        return lockFaceMapper.selectLockFaceByLockIdAndUserName(lockId, userName);
    }

    @Override
    public List<LockFace> selectLockListByIdentifications(List<String> identifications) {
        return lockFaceMapper.selectLockListByIdentifications(identifications);
    }

    @Override
    @Transactional
    public AjaxResult updateLockMainAdmin(LockFace lockFace, Long loginUserId) {
        if (lockFace.getLockId() == null || lockFace.getFaceId() == null) {
            return AjaxResult.error("数据异常，请联系管理员");
        }
        LockFace byFaceId = lockFaceMapper.selectLockFaceByFaceId(lockFace.getFaceId());
        if (ObjectUtil.isEmpty(byFaceId)) {
            return AjaxResult.error("授权用户数据异常，请联系管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.LOCK_STATE_FORBIDDEN, byFaceId.getFaceState())) {
            return AjaxResult.error("该用户已被禁用,请先启用该用户");
        }
        DeviceLock deviceLock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(deviceLock)) {
            return AjaxResult.error("锁不存在，请联系管理员");
        }
        LockFace face;
        if (loginUserId == null) {
            face = lockFaceMapper.selectLockFaceByLockIdByMainAdmin(lockFace.getLockId());
            if (ObjectUtil.isEmpty(face)) {
                return AjaxResult.error("主管理员数据异常");
            }
        } else {
            face = lockFaceMapper.selectLockFaceByLockIdAndUserId(lockFace.getLockId(), loginUserId);
            if (ObjectUtil.isEmpty(face) || !StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, face.getAuthRoleType())) {
                return AjaxResult.error("当前用户没有变更该锁的角色权限");
            }
        }
        Date nowDate = DateUtils.getNowDate();
        face.setUpdateTime(nowDate);
        face.setAuthRoleType(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN);
        lockFace.setUpdateTime(nowDate);
        lockFace.setAuthRoleType(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN);
        List<LockFace> list = Arrays.asList(lockFace, face);
        lockFaceMapper.updateBatchLockFace(list);

        // 添加操作记录
        lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), face.getUserName(),
                DeviceLockConstants.OPERATING_TYPE_EDIT, face.getUserName() + "将主管理员权限变更给" + byFaceId.getUserName());
        return AjaxResult.success();
    }

    @Override
    public LockFace selectLockFaceByLockIdAndUserId(Long lockId, Long userId) {
        return lockFaceMapper.selectLockFaceByLockIdAndUserId(lockId, userId);
    }

    @Override
    @Transactional
    public AjaxResult editFaceState(Long faceId, Long loginUserId) {
        //被操作人的授权信息
        LockFace lockFace = lockFaceMapper.selectLockFaceByFaceId(faceId);
        if (ObjectUtil.isEmpty(lockFace)) {
            return AjaxResult.error("人脸授权不存在，请联系管理员");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MAIN_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("不能操作主管理员");
        }
        if (StrUtil.isBlank(lockFace.getIdentification())) {
            return AjaxResult.error("请先完善授权信息");
        }
        //操作人的授权信息
        LockFace face = lockFaceMapper.selectLockFaceByLockIdAndUserId(lockFace.getLockId(), loginUserId);
        if (ObjectUtil.isEmpty(face) ||
                StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MEMBER, face.getAuthRoleType())) {
            return AjaxResult.error("当前用户没有操作权限");
        }
        if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, face.getAuthRoleType()) &&
                StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, lockFace.getAuthRoleType())) {
            return AjaxResult.error("管理员不能操作管理员");
        }
        DeviceLock lock = deviceLockService.selectDeviceLockByLockId(lockFace.getLockId());
        if (ObjectUtil.isEmpty(lock)) {
            return AjaxResult.error("锁不存在");
        }
        if (StrUtil.equals(DeviceLockConstants.LOCK_STATE_ENABLE, lockFace.getFaceState())) {
            lockFace.setFaceState(DeviceLockConstants.LOCK_STATE_FORBIDDEN);
            lockFace.setForbiddenUserId(loginUserId);
            lockPwdService.removeLockPwd(lock, lockFace);
            lockDevicesService.removeFace(lock.getLockMac(), lockFace.getIdentification());
        } else if (StrUtil.equals(DeviceLockConstants.LOCK_STATE_FORBIDDEN, lockFace.getFaceState())) {
            if (StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_ADMIN, face.getAuthRoleType()) &&
                    !Objects.equals(lockFace.getForbiddenUserId(), face.getUserId())) {
                return AjaxResult.error("只能启用自己的禁用的授权信息");
            }
            lockFace.setFaceState(DeviceLockConstants.LOCK_STATE_ENABLE);
            lockPwdService.createLockPwd(lock, lockFace);
            lockDevicesService.createLockFace(lockFace.getFaceImage(), lock.getLockMac(), lockFace.getIdentification(),
                    lockFace.getAuthStartTime(), lockFace.getAuthEndTime());
        }
        int result = lockFaceMapper.updateLockFace(lockFace);
        if (result > 0) {
            lockOperatingRecordService.addLockOperatingRecord(lockFace.getLockId(), face.getUserName(),
                    DeviceLockConstants.OPERATING_TYPE_EDIT, face.getUserName() + DefaultEnum.getLabelByValue(lockFace.getFaceState()) + "了" + lockFace.getUserName() + "的授权");
        }
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public List<LockFace> selectLockFaceByAuthRoleTypeAndUserId(String authRoleType, Long userId) {
        return lockFaceMapper.selectLockFaceByAuthRoleTypeAndUserId(authRoleType, userId);
    }

    @Override
    public List<LockFace> selectLockFaceByUserId(Long userId) {
        return lockFaceMapper.selectLockFaceByUserId(userId);
    }

    @Override
    public LockFace selectLockFaceByLockIdByMainAdmin(Long lockId) {
        return lockFaceMapper.selectLockFaceByLockIdByMainAdmin(lockId);
    }

    @Override
    public int softDeleteLockFaceByUserId(Long userId) {
        return lockFaceMapper.softDeleteLockFaceByUserId(userId);
    }

    @Override
    public int softDeleteLockFaceByLockId(Long lockId) {
        return lockFaceMapper.softDeleteLockFaceByLockId(lockId);
    }

    public AjaxResult checkParam(LockFace lockFace) {
        if (lockFace.getLockId() == null) {
            return AjaxResult.error("锁ID不能为空");
        }
        /*if (StrUtil.isBlank(lockFace.getFaceImage())) {
            return AjaxResult.error("请上传人脸图片");
        }*/
        if (!StrUtil.equals(DeviceLockConstants.AUTH_ROLE_TYPE_MEMBER, lockFace.getAuthRoleType()) &&
                StrUtil.isBlank(lockFace.getUserPhone())) {
            return AjaxResult.error("手机号不能为空");
        }

        List<Map<String, Object>> maps = lockFaceMapper.checkNameAndPhone(lockFace);
        if (CollectionUtil.isNotEmpty(maps)) {
            for (Map<String, Object> map : maps) {
                if (Integer.parseInt(map.get("num").toString()) > 0) {
                    String type = map.get("type").toString();
                    switch (type) {
                        case "1":
                            return AjaxResult.error("名称已存在");
                        case "2":
                            return AjaxResult.error("该手机号已添加");
                    }
                }
            }
        }
        return null;
    }

    @Override
    public int insertBatchLockFace(List<LockFace> lockFaceList) {
        return lockFaceMapper.insertBatchLockFace(lockFaceList);
    }

    @Override
    public List<LockFace> userIdRelevanceDevice(LockFace lockFace) {
        return lockFaceMapper.userIdRelevanceDevice(lockFace);
    }
}
